/*
test http link :
http://demo.a.diyeasy.com.cn/admin/Interactive/interative.jsp
 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/unistd.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "protocol.h"
#include "apphead.h"

/* debugl  */
#define  OS_USING_DEBUGE
#ifdef OS_USING_DEBUGE
#define dprintf			printf
#else
#define dprintf(...)		
#endif


#define HTTP_POST		"POST /admin/mdh.action HTTP/1.1\n"
#define HTTP_HOST               "Host: demo.a.diyeasy.com.cn\n"

#define IPSTR	 		"120.24.229.246"
#define PORT 			80 
#define BUFSIZE 		(1024*4)

/**/
#define DEV_ARG_LEN             32
#define HTTP_TAG_LEN            1024 

/**/
#define UOD_ARG_NAME            "UID="
#define PASS_ARG_NAME		"&PASS="
#define WAPID_ARG_NAME	        "&WAPID="			
#define DEVID_ARG_NAME          "&DEVID="
#define CMD_ARG_NAME            "&CMD="
#define TAG1_ARG_NAME           "&TAG1="
#define TAG2_ARG_NAME           "&TAG2="
#define TAG3_ARG_NAME           "&TAG3="

	
typedef struct 
{
	unsigned char UOD[DEV_ARG_LEN];
	unsigned char PASS[DEV_ARG_LEN];
	unsigned char WAPID[DEV_ARG_LEN];
	unsigned char DEVID[DEV_ARG_LEN];
	unsigned char CMD[DEV_ARG_LEN];
	unsigned char TAG1[HTTP_TAG_LEN];
	unsigned char TAG2[HTTP_TAG_LEN];
	unsigned char TAG3[HTTP_TAG_LEN];
}HttpMsgArgDef;

void http_arg_to_string(char *str,HttpMsgArgDef *HttpReq)
{
	char *buf; 

	OS_ASSERT(str);
	OS_ASSERT(HttpReq);
	OS_ASSERT(HttpReq->UOD);
	OS_ASSERT(HttpReq->PASS);
	OS_ASSERT(HttpReq->WAPID);
	OS_ASSERT(HttpReq->DEVID);
	OS_ASSERT(HttpReq->CMD);
	OS_ASSERT(HttpReq->TAG1);
	OS_ASSERT(HttpReq->TAG2);
	OS_ASSERT(HttpReq->TAG3);
	
	buf = str;

	// set uod
	strncpy(buf,UOD_ARG_NAME,strlen(UOD_ARG_NAME));
	buf += strlen(UOD_ARG_NAME);

	strncpy(buf,HttpReq->UOD,strlen(HttpReq->UOD));
	buf += strlen(HttpReq->UOD);

	// set pass
	strncpy(buf,PASS_ARG_NAME,strlen(PASS_ARG_NAME));
	buf += strlen(PASS_ARG_NAME);

	strncpy(buf,HttpReq->PASS,strlen(HttpReq->PASS));
	buf += strlen(HttpReq->PASS);

	// set wapid
	strncpy(buf,WAPID_ARG_NAME,strlen(WAPID_ARG_NAME));
	buf += strlen(WAPID_ARG_NAME);

	strncpy(buf,HttpReq->WAPID,strlen(HttpReq->WAPID));
	buf += strlen(HttpReq->WAPID);
	
	// set devid
	strncpy(buf,DEVID_ARG_NAME,strlen(DEVID_ARG_NAME));
	buf += strlen(DEVID_ARG_NAME);

	strncpy(buf,HttpReq->DEVID,strlen(HttpReq->DEVID));
	buf += strlen(HttpReq->DEVID);

	// set cmd
	strncpy(buf,CMD_ARG_NAME,strlen(CMD_ARG_NAME));
	buf += strlen(CMD_ARG_NAME);

	strncpy(buf,HttpReq->CMD,strlen(HttpReq->CMD));
	buf += strlen(HttpReq->CMD);

	// set tag1
	strncpy(buf,TAG1_ARG_NAME,strlen(TAG1_ARG_NAME));
	buf += strlen(TAG1_ARG_NAME);

	strncpy(buf,HttpReq->TAG1,strlen(HttpReq->TAG1));
	buf += strlen(HttpReq->TAG1);

	// set tag2
	strncpy(buf,TAG2_ARG_NAME,strlen(TAG2_ARG_NAME));
	buf += strlen(TAG2_ARG_NAME);

	strncpy(buf,HttpReq->TAG2,strlen(HttpReq->TAG2));
	buf += strlen(HttpReq->TAG2);

	// set tag3
	strncpy(buf,TAG3_ARG_NAME,strlen(TAG3_ARG_NAME));
	buf += strlen(TAG3_ARG_NAME);

	strncpy(buf,HttpReq->TAG3,strlen(HttpReq->TAG3));
	buf += strlen(HttpReq->TAG3);
        dprintf("string :%s\n",str);
}

int send_http_request(HttpMsgArgDef *HttpReq,unsigned char *ack, int method) 
{
	int 			sockfd, ret, i, h;
	struct sockaddr_in 	servaddr;
	char *str1, *str2, *buf, *str;
        char str3[4906];
	socklen_t               len;
	fd_set                  t_set1;
	struct timeval          tv;
        int                     outtime;

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
	{
		printf("create scoket fail---socket error!\n");
		exit(0);
	};

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(PORT);
	if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 )
	{
		printf("create inet_pton fail ---inet_pton error!\n");
		exit(0);
	};

	if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
	{
		printf("create connect fail ---connect error!\n");
		exit(0);
	}
	printf("connect server ok...\n");

	//send data
	str2=(char *)malloc(4096);
	str1=(char *)malloc(4096);
        OS_ASSERT(str2);
        OS_ASSERT(str1);

	memset(str2, 0, 4096);
	http_arg_to_string(str2,HttpReq);
        dprintf("str2:%d\n",str2);


	str=(char *)malloc(512);
	len = strlen(str2);
	sprintf(str, "%d", len);

	memset(str1, 0, 4096);
	strcat(str1, HTTP_POST);
	strcat(str1, HTTP_HOST);
	strcat(str1, "Content-Type: application/x-www-form-urlencoded\n");
	strcat(str1, "Content-Length: ");
	strcat(str1, str);
	strcat(str1, "\n\n");

	strcat(str1, str2);
	strcat(str1, "\r\n\r\n");
	printf("%s\n",str1);

	ret = write(sockfd,str1,strlen(str1));
	if (ret < 0) {
		printf("send data fail!!! error num is:%d,error info'%s'\n",errno, strerror(errno));
		exit(0);
	}else{
		printf("send data ok^_^,send size:%d Byte!\n\n", ret);
	}

	FD_ZERO(&t_set1);
	FD_SET(sockfd, &t_set1);
        free(str);
        free(str1);
        free(str2);
        
	buf=(char *)malloc(BUFSIZE);
        outtime = 0;
	while(1)
	{
                //sleep(1);
                usleep(1000*300);

		tv.tv_sec= 0;
		tv.tv_usec= 0;

		h= 0;
                printf("--------------->1\n");
		h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
                printf("--------------->2\n");

		if (h == 0)
		{
                        dprintf("read out time %d\n",outtime);
                        if(outtime++ > 10)
                        {
                                break; 
                        }
			continue;			
		};
		if (h < 0)
		{
			close(sockfd);
			printf("read http data abnormal!!!\n");
                        free(buf);
			return -1;
		};

		if (h > 0)
		{
                        dprintf("have http data\n");
			memset(buf, 0, 4096);
			i= read(sockfd, buf, 4095);
			if (i==0)
			{
				close(sockfd);
				printf("read http data ,but scoket connet is colse!\n");
				return -1;
			}

			printf(">>>>>>>>\n");
			printf("%s\n", buf);
                        if(strstr(buf,"\r\n\r\n") != NULL)
                        {
                                char *temp;

                                temp = strstr(buf,"\r\n\r\n");
                                temp += 4;
                                memcpy(ack,temp,strlen(temp)); 
                                dprintf("ack: %s\n",ack);
                        }
                        break;
		}
	}
	close(sockfd);
        dprintf("http connet colse\n");
        free(buf);
        
	return 0;
}

void protocol_thread_process(void)
{
	HttpMsgArgDef httptest;
	char *str=(char *)malloc(4096);
	char *buf=(char *)malloc(BUFSIZE);

	strcpy(httptest.UOD,"tianheting");	
	strcpy(httptest.PASS,"10010");	
	strcpy(httptest.WAPID,"1d");	
	strcpy(httptest.DEVID,"0");	
	strcpy(httptest.CMD,"ReadTime");	
	strcpy(httptest.TAG3,"12");	

	// test string deal fun
	http_arg_to_string(str,&httptest);
	dprintf("str :%s \n",str);
		
        memset(buf,0,BUFSIZE);
	send_http_request(&httptest,buf,0);
        sleep(1);
        dprintf("buf :%s\n@@@@",buf);
        free(buf);
        dprintf("##########"); 

}


